<?php
include 'attendance_lib.php';

$a_css_files = array (
    array ( 'path' => 'plugins/jquery-ui/jquery-ui-1.10.1.custom.min.css' ),
    array ( 'path' => 'plugins/bootstrap-modal/css/bootstrap-modal.css' ),
);

$a_js_files = array (
    array ( 'path' => 'plugins/bootstrap-modal/js/bootstrap-modal.js' ),
    array ( 'path' => 'plugins/bootstrap-modal/js/bootstrap-modalmanager.js' ),
    array ( 'path' => 'scripts/nh-attendance.js' ),
);

$a_js_class[] = 'Attendance.init();';

// <editor-fold defaultstate="collapsed" desc="Get Data">
$a_user = $a_sundays = $a_attendances = array ();
$s_Where = $v_startdate = $v_enddate = '';

// <editor-fold defaultstate="collapsed" desc="Make Where">
if ($s_category_team) {
    
    $v_tmp_date  = date('Y-m-01', strtotime($s_category4));
    $v_startdate = date('Y-m-d' , strtotime('-7 day', strtotime($v_tmp_date)));
    $v_enddate   = date('Y-m-d' , strtotime('+1 month -1 day', strtotime($v_tmp_date)));
    
    if ( strtotime($v_startdate) < strtotime($a_KhoaHoc_Info['start'])) { $v_startdate = $a_KhoaHoc_Info['start']; }
    if ( strtotime($v_enddate  ) > strtotime($a_KhoaHoc_Info['end'  ])) { $v_enddate   = $a_KhoaHoc_Info['end'  ]; }
    
    $a_sundays        = getDateForSpecificDayBetweenDates($v_startdate, $v_enddate);
    $a_hidden_columns = getDateForSpecificDayBetweenDates(date('Y-m-d' , strtotime('-7 day')), date('Y-m-d')); //Only Show current sundate
    
    $db_query = "
        select  userid
        from    user_class
        where   course = '" . $a_KhoaHoc_Info['start_year'] . "' and team = '$s_category_team' and level = '$s_category_level' and crew = '$s_category_crew' ";
    
    $db_result = db_query($db_conn, $db_query);

    while ($d_row = db_fetch($db_result)) {
        $a_user[$d_row->userid] = $d_row->userid;
    }
    
    $s_Where = $s_Where  . ' and userid in ("' . implode('", "', $a_user) . '")';
}

make_where($s_Where, $a_filterings);
// </editor-fold>

if (!empty($a_user)) {
    $db_query  = db_get_query($a_tablecolumns_namhoa_user) . $s_Where . " and $s_get_THIEU_NHI_condition " . ' order by firstname';
    
    $db_result = db_query($db_conn, $db_query);

    $a_user = array ( );
    
    while ($d_row = db_fetch($db_result)) {
        $a_user[$d_row->userid] = $d_row;
    }

    if (!empty($a_user)) {

        $db_query = "
            select  userid, scoretype, date, score
            from    user_score
            where   userid in ('" . implode("', '", array_keys($a_user)) . "') and date >= '$v_startdate' and date <= '$v_enddate'
                    and type = 'CHUYEN_CAN'";

        $db_result = db_query($db_conn, $db_query);

        while ($d_row = db_fetch($db_result)) {
            $a_attendances[$d_row->userid][$d_row->date][$d_row->scoretype] = $d_row->score;
        }
    }
}

$a_permission = get_passport_action_from_module($db_conn, array('DIEM_DANH'));
// </editor-fold>

include 'lib_head.php';
?>
<!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<div id="portlet-config" class="modal hide">
    <div class="modal-header">
        <button data-dismiss="modal" class="close" type="button"></button>
        <h3>Bộ Lọc</h3>
    </div>
    <div class="modal-body">
        Điền thông tin cần thiết để lọc được dữ liệu thích hợp.
    </div>
</div>
<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<div class="row-fluid">
    <!-- BEGIN BORDERED TABLE PORTLET-->
    <div class="portlet box green">
        <div class="portlet-title">
            <div class="caption"><i class="icon-filter"></i>Lọc Dữ Liệu</div>
            <div class="tools">
                <a href="javascript:;" class="collapse"></a>
                <a href="#portlet-config" data-toggle="modal" class="config"></a>
            </div>
        </div>
        <div class="portlet-body">
            <form action="" style="margin: 0">
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Ngành</label>
                        <div class="controls">
                            <select name="category_team" class="m-wrap span12">
                                <option value=""></option>
                                <?php foreach ($a_Nganh as $v_key => $v_value) { ?>
                                    <option <?= $s_category_team == $v_key ? 'selected="selected"' : '' ?> value="<?= $v_key ?>"><?= $v_value ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Cấp</label>
                        <div class="controls">
                            <select name="category_level" class="m-wrap span12">
                                <option value=""></option>
                                <?php foreach ($a_Cap as $v_key => $v_value) { ?>
                                    <option <?= $s_category_level == $v_key ? 'selected="selected"' : '' ?> value="<?= $v_key ?>"><?= $v_value ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Đội</label>
                        <div class="controls">
                            <select name="category_crew" class="m-wrap span12">
                                <option value=""></option>
                                <?php foreach ($a_Doi as $v_key => $v_value) { ?>
                                    <option <?= $s_category_crew == $v_key ? 'selected="selected"' : '' ?> value="<?= $v_key ?>"><?= $v_value ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label">Tháng</label>
                        <div class="controls">
                            <input type="text" name="category4" value="<?= $s_category4 ? $s_category4 : date('Y-m') ?>" class="m-wrap medium span12 monthpicker"/>
                        </div>
                    </div>
                </div>
                <div class="pull-left">
                    <div class="control-group">
                        <label class="control-label">&ensp;</label>
                        <div class="controls">
                            <div class="input-prepend">
                                <button class="btn blue">BẮT ĐẦU LỌC <i class="m-icon-swapright m-icon-white"></i></button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="clearfix"></div>
            </form>
        </div>
    </div>
    <!-- END BORDERED TABLE PORTLET-->
    <div class="portlet-body">
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th rowspan="2" style="vertical-align: middle">Họ và Tên</th>
                    <?php foreach ($a_sundays as $v_sunday) : ?>
                    <th colspan="2" style="text-align: center" class="<?=!in_array($v_sunday, $a_hidden_columns) ? 'hidden-phone' : ''?>"><?= namhoa_date($v_sunday) ?></th>
                    <?php endforeach; ?>
                </tr>
                <tr>
                    <?php foreach ($a_sundays as $v_sunday) {
                        foreach ($a_Loai_ChuyenCan as $v_value) { ?>
                    <th style="text-align: center" class="<?=!in_array($v_sunday, $a_hidden_columns) ? 'hidden-phone' : ''?>"><?= $v_value ?></th>
                    <?php } } ?>
                </tr>
            </thead>
            <?php foreach ($a_user as $v_counter => $d_user) : ?>
            <tbody>
                <tr>
                    <td><span class="hidden-phone"><?=$d_user->saint?></span> <?=$d_user->username?></td>
                    <?php foreach ($a_sundays as $v_sunday) {
                        $v_tmp_le  = isset($a_attendances[$d_user->userid][$v_sunday]['LE' ]) ? $a_attendances[$d_user->userid][$v_sunday]['LE' ] : '';
                        $v_tmp_hoc = isset($a_attendances[$d_user->userid][$v_sunday]['HOC']) ? $a_attendances[$d_user->userid][$v_sunday]['HOC'] : ''; ?>
                    <td class="<?=!in_array($v_sunday, $a_hidden_columns) ? 'hidden-phone' : ''?> <?=!empty($a_permission) ? 'dile'  : '' ?>" score="<?=$v_tmp_le ?>" date="<?=$v_sunday?>" userid="<?=$d_user->userid?>" username="<?=$d_user->username?>" style="text-align: center;"><?=@$a_DiLe [$v_tmp_le ]?></td>
                    <td class="<?=!in_array($v_sunday, $a_hidden_columns) ? 'hidden-phone' : ''?> <?=!empty($a_permission) ? 'dihoc' : '' ?>" score="<?=$v_tmp_hoc?>" date="<?=$v_sunday?>" userid="<?=$d_user->userid?>" username="<?=$d_user->username?>" style="text-align: center;"><?=@$a_DiHoc[$v_tmp_hoc]?></td>
                    <?php } ?>
                </tr>
            </tbody>
            <?php endforeach; ?>
        </table>
    </div>
</div>
<div id="responsive" class="modal hide fade" tabindex="-1"></div>
<?php include 'lib_tail.php'; ?>